package com.leoman.controller.common;

import com.leoman.controller.editor.*;
import com.leoman.core.Constant;
import com.leoman.entity.VideoRecord;
import com.leoman.utils.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共控制层
 * Created by 涂奕恒 on 2014/12/10.
 */
public class CommonController {

    public static Map<String, Object> emptyData = null;

    static {
        emptyData = new HashMap<>();
        emptyData.put("data", new ArrayList());
        emptyData.put("iTotalRecords", 0);
        emptyData.put("iTotalDisplayRecords", 0);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new CustomStringEditor());
        binder.registerCustomEditor(MultipartFile.class, new CustomFileEditor());
        binder.registerCustomEditor(Double.class, new CustomDoubleEditor());
        binder.registerCustomEditor(Float.class, new CustomFloatEditor());
        binder.registerCustomEditor(Integer.class, new CustomIntegerEditor());
        binder.registerCustomEditor(Long.class, new CustomLongEditor());
        binder.registerCustomEditor(Date.class, new CustomDateEditor());
    }

    public Integer getPageNum(Integer start, Integer length) {
        if (start == null) {
            start = 0;
        }
        if (length == null) {
            length = 10;
        }

        return (start / length) + 1;
    }

    /**
     * 获取 or 转化分页需要的参数
     *
     * @param pageNum  页码
     * @param pageSize 每页显示条数
     * @return 数组
     */
    public static Integer[] getPageParams(Integer pageNum, Integer pageSize) {
        Integer[] info = new Integer[2];

        if (null == pageNum || pageNum < 0) {
            pageNum = 1;
        }

        if (null == pageSize) {
            pageSize = Constant.PAGE_DEF_SIZE;
        }

        if (pageNum > pageSize) {
            pageNum = pageSize;
        }

        info[0] = pageNum;
        info[1] = pageSize;

        return info;
    }

    /**
     * 根据区域id字符串获取对应的区域信息集合
     *
     * @param regionIds 需要解析的区域id字符串
     * @return 区域信息集合
     */
    /*public static List<RegionVo> getRegionInfoList(String regionIds, RegionDao regionDao) {
        List<RegionVo> list = new ArrayList<>();

        if (StringUtils.isBlank(regionIds)) {
            return list;
        }

        String[] ids = regionIds.replaceAll("@", "").split(",");
        RegionVo regionVo;
        Region region;

        for (String id : ids) {
            if (StringUtils.isBlank(id)) {
                continue;
            }

            region = regionDao.findOne(Integer.parseInt(id));

            regionVo = new RegionVo();
            regionVo.setId(region.getId());
            regionVo.setName(region.getName());

            list.add(regionVo);
        }

        return list;
    }*/

    /**
     * 根据视频投放记录信息解析区域名称并拼接
     *
     * @param videoRecord 需要解析的视频投放记录信息
     * @return 区域信息集合
     */
    public static String getRegionNames(VideoRecord videoRecord) {
        String regionNames = "";

        if (null == videoRecord) {
            return regionNames;
        }

        String[] names = videoRecord.getRegionNames().split(",");

        for (String name : names) {
            regionNames += name + ",";
        }

        if (regionNames.length() > 0) {
            regionNames = regionNames.substring(0, regionNames.length() - 1);
        }

        return regionNames;
    }

    public static Long getFirstTime(Integer index) {
        if (null == index) {
            index = Constant.days;
        }
        Date date = DateUtils.getDateWithDay(index * -1, 0);
        return date.getTime();
    }
}
